package com.edou.cms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.cms.dao.ICmsBlogArticleDao;
import com.edou.cms.entity.CmsBlogArticle;
import com.edou.cms.service.ICmsBlogArticleService;

import com.edou.cms.dto.CmsArticleCondition;
import com.edou.cms.dto.CmsBlogArticleCondition;

@Service("cmsBlogArticleServiceImpl")
@Transactional(value = "transactionManager")
public class CmsBlogArticleServiceImpl implements ICmsBlogArticleService{

	@Resource(name = "cmsBlogArticleDaoImpl")
	private ICmsBlogArticleDao cmsBlogArticleDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listCmsBlogArticleByPage(PageResult<CmsBlogArticle> pageResult,CmsBlogArticleCondition condition){
		int rows = cmsBlogArticleDao.listCmsBlogArticleByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<CmsBlogArticle> list = cmsBlogArticleDao.listCmsBlogArticleByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 博客内的相关文章
	 */
	public CmsBlogArticle getCmsBlogArticleById(String id){
		return cmsBlogArticleDao.getCmsBlogArticleById(id);
	}

	/**
	 * 新增
	 * @param item 博客内的相关文章
	 */
	public void add(CmsBlogArticle item){
		cmsBlogArticleDao.add(item);
	}

	/**
	 * 修改
	 * @param item 博客内的相关文章
	 */
	public void update(CmsBlogArticle item){
		cmsBlogArticleDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		cmsBlogArticleDao.delByIds(ids);
	}

	/**
	 * 根据文章id查询
	 */
	public CmsBlogArticle getCmsBlogArticleByArticleId(String articleId) {
		return cmsBlogArticleDao.getCmsBlogArticleByArticleId(articleId);
	}


	public List<CmsBlogArticle> listCmsBlogArticleByAuthorId(CmsArticleCondition condition) {
		RowBounds rowBounds = new RowBounds(condition.getCurrentIndex(),condition.getPageSize());
		return cmsBlogArticleDao.listCmsBlogArticleByAuthorId(rowBounds,condition.getAuthorId());
	}

	/**
	 * 根据博主id查询前4篇文章 
	 */
	public List<CmsBlogArticle> listCmsBlogArticleByAuthorIdTopFour(String authorId) {
		return cmsBlogArticleDao.listCmsBlogArticleByAuthorIdTopFour(authorId);
	}

	@Override
	public void delByArticleId(String articleId) {
		cmsBlogArticleDao.delByArticleId(articleId.split(","));
	}

	@Override
	public List<CmsBlogArticle> listCmsBlogArticleByAuthorId(String id) {
		RowBounds rowBounds = new RowBounds();
		return cmsBlogArticleDao.listCmsBlogArticleByAuthorId(rowBounds, id);
	}

}

